﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class TDictTypeRepository : BaseRepository<TDictType>, ITDictTypeRepository
    {
        public TDictTypeRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }
        /// <summary>
        /// 添加字典类型
        /// </summary>
        /// <param name="dictTypeDto"></param>
        /// <returns></returns>
        public bool AddTDictType(TDictType dic)
        {
            try
            {
                string code = "DictType";
                var aobj = Context.TDictTypes.AsNoTracking().Where(x => x.TypeCode.Contains(code)).OrderByDescending(x => x.CreateDate).FirstOrDefault();
                if (aobj != null)
                {
                    string pattern = @"\d+";
                    MatchCollection matches = Regex.Matches(aobj.TypeCode, pattern);
                    string value = matches[matches.Count - 1].Value;
                    int values = Convert.ToInt32(value);
                    values++;
                    dic.TypeCode = code + values.ToString("D4");
                }
                else
                {
                    int i = 1;
                    dic.TypeCode = code + i.ToString("D4");
                }

                var tobj = Context.TDictTypes.AsNoTracking().FirstOrDefault(x => x.TypeCode == dic.TypeCode);
                if (tobj!=null)
                {
                    return false;
                }
                dic.CreateUserId = dic.NowUserId;
                dic.CreateUserName = dic.NowUserRealName;
                dic.CreateDate = DateTime.Now;
                dic.ModifyDate = DateTime.Now;

                Context.TDictTypes.Add(dic);
                return Context.SaveChanges() > 0;
            }
            catch (Exception)
            {

                throw;
            }
       
        }
        /// <summary>
        /// 删除字典类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        public bool DelTDicType(int id)
        {
            try
            {
                var dic = Context.TDictTypes.Find(id);
                if (dic is not null)
                {
                    Context.TDictTypes.Remove(dic);
                    return Context.SaveChanges() > 0;
                }
                return false;
            }
            catch (Exception)
            {

                throw;
            }

        }

        /// <summary>
        /// 查询字典类型
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>

        public List<TDictType> GetAllTDictType(string? keyword, int pageIndex,int pageSize,out int totalCount)
        {
            try
            {
                var dicList = Context.TDictTypes.AsQueryable();
                if (!string.IsNullOrEmpty(keyword))
                {
                    dicList = dicList.Where(c => c.TypeName.Contains(keyword));
                }
                totalCount = dicList.Count();
                dicList = dicList.OrderBy(x => x.DisplayOrder).Skip((pageIndex - 1) * pageSize).Take(pageSize);
                return dicList.ToList();
            }
            catch (Exception)
            {

                throw;
            }
  
        }
        /// <summary>
        /// 是否查询树形字典的字典类型
        /// </summary>
        /// <param name="isTree">是否要查询树形字典类型</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<TDictType> GetAllTDictTypeTree(string? keyword, bool isTree)
        {
            try
            {
                var res = Context.TDictTypes.Where(d => d.IsTree);
                if (!isTree)
                {
                    res = Context.TDictTypes.Where(d => d.IsTree == false);
                }
                if (!string.IsNullOrEmpty(keyword))
                {
                    res = res.Where(c => c.TypeName.Contains(keyword));
                }
                return res.ToList();
            }
            catch (Exception)
            {

                throw;
            }

        }

        /// <summary>
        /// 根据id查询数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public TDictType GetTDictTypeByID(int id)
        {
            try
            {
                return Context.TDictTypes.Find(id);
            }
            catch (Exception)
            {

                throw;
            }

        }

        /// <summary>
        /// 修改字典类型
        /// </summary>
        /// <param name="dictType"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool UpdTDicType(TDictType dictType)
        {
            try
            {
                var dobj = Context.TDictTypes.AsNoTracking().FirstOrDefault(x => x.Id != dictType.Id && x.TypeCode == dictType.TypeCode);
                if(dobj!=null)
                {
                    return false;
                }
                dictType.ModifyDate = DateTime.Now;
                dictType.ModifyUserId = dictType.NowUserId;
                dictType.ModifyUserName = dictType.NowUserRealName;
                Context.TDictTypes.Update(dictType);
                return Context.SaveChanges() > 0;
            }
            catch (Exception)
            {

                throw;
            }

        }
    }
}
